import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;

//作业1
class School implements Serializable {
    private String name;
    private String address;
    private int year;

    public School(String name, String address, int year) {
        this.name = name;
        this.address = address;
        this.year = year;
    }

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", year=" + year +
                '}';
    }

    public void s1() {
        System.out.println("学校姓名："+this.name+" "+"地址："+this.address+" "+"成立的年份："+this.year);

    }
}
//作业2

class Student implements Comparable<Student>{
    private int sid;
    private String name;
    private int age;

    public Student(int sid, String name, int age) {
        this.sid = sid;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        if (this.sid<o.sid){
            return -1;
        }else if (this.sid==o.sid){
           if(this.age<o.age){
               return -1;
           }
        }
    return 1;
    }
}
class MyCompartor implements Comparator<Student> {
    @Override
    public int compare(Student s1, Student s2) {
        if (s1.getSid() < s2.getSid()) {
            return -1;
        } else if (s1.getSid() == s2.getSid()) {
            if (s1.getAge() < s2.getAge()) {
                return -1;
            }
        }
        return 1;
    }
}

public class test7 {
    public static void main(String[] args) throws IOException {
        //作业1;
        //writeToFile();
        //readFromFile();
        //作业2；
        作业2();
    }

    private static void 作业2() {
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student(6, "一", 20));
        set.add(new Student(4, "二", 18));
        set.add(new Student(3, "三", 19));
        set.stream().forEach(System.out::println);
        System.out.println("------------------");
        TreeSet<Student> set1 = new TreeSet<>(new MyCompartor());
        set1.add(new Student(6, "uii", 20));
        set1.add(new Student(6, "一·", 18));
        set1.add(new Student(3, "二", 19));
        set1.stream().forEach(System.out::println);
    }

    private static void readFromFile() throws IOException {
     ObjectInputStream ois =new ObjectInputStream(new FileInputStream("day1//1.dat"));
      try{
          while (true){
              Object obj =ois.readObject();
              if(obj instanceof School){
                  ((School) obj).s1();
              }
              System.out.println(obj);
          }
      } catch (EOFException | ClassNotFoundException e) {
          System.out.println("文件读取结束！");
      }
      ois.close();
    }

    private static void writeToFile() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day1//1.dat"));
        School school1 =new School("上海交大","上海",2002);
        School school2 =new School("泉州职业技术大学","泉州",2010);
        oos.writeObject(school1);
        oos.writeObject(school2);
        oos.close();
        System.out.println("写入成功！");
    }
}
